package edu.byu.intex2.data.dao;

import edu.byu.intex2.data.DataException;
import edu.byu.intex2.data.bo.*;
import java.sql.*;
import java.util.List;
import java.util.LinkedList;

/**
 * A singleton object that CRUD's ServiceLine objects.
 *
 * @author Conan C. Albrecht <conan@warp.byu.edu>
 * @version 2008-03-03
 */
public class ServiceLineDAO  {

  /////////////////////////////////////////////
  ///   Singleton code

  private static ServiceLineDAO instance = null;

  /** Creates a new instance of ServiceLineDAO */
  private ServiceLineDAO() {
  }//constructor

  /** Retrieves the single instance of this class */
  public static synchronized ServiceLineDAO getInstance() {
    if (instance == null) {
      instance = new ServiceLineDAO();
    }
    return instance;
  }//getInstance


  ////////////////////////////////////////////
  ///   CREATE methods

  /** Creates a new serviceLine in the database */
  public ServiceLine create() throws DataException {
	//create a new blank serviceLine object without any parameters in the constructor
	//set the alreadyInDB variable to false
	//put to cache and return the new object
  }



  ////////////////////////////////////////////
  ///   READ methods

  /** Reads an existing serviceLine from the database */
  public ServiceLine read(String id) throws DataException {
    //check to see if the object is in cache, if it is just return it
	//establish a connection from the pool, then pass that connection along with the string id to the next method
	//catch any exceptions you may have
	//return the serviceLine
	//RELEASE THE CONNECTION!!
  }

  /** Internal method to read an existing serviceLine from the database */
  synchronized ServiceLine read(String id, Connection conn) throws Exception {
    //check the cache just like above just to make sure
	//use a prepared statement to prevent against sql injection
	//run the query and get the result set. have the next method process the result set
	//catch any exceptions you may have
	//return the serviceLine

  }//read


  /** Internal method to create a serviceLine object from a record */
  synchronized ServiceLine readRecord(ResultSet rs, Connection conn) throws Exception {
    //again, check the cache, but check it with the id from the record set that was passed
	//create a new serviceLine object. don't forget to put it to cache, set it's alreadyInDB param to true and dirty to false
	//return the serviceLine

  }//readRecord



  /////////////////////////////////////////////
  ///   UPDATE methods

  /** Saves an existing serviceLine in the database */
  public void save(ServiceLine serviceLine) throws DataException {
    //get a connection from the pool, save all of the serviceLines first using the other serviceLine method and then commit them in the database
	//rollback if there were any errors saving, catch any other errors you might have
	//release the connection!!

  }

  /** Internal method to update a serviceLine in the database */
  void save(ServiceLine serviceLine, Connection conn) throws Exception {
    //check cache first, if returns nothing, put the serviceLine object to cache
	//decide whether to update or insert into the database
	//let the ServiceLineLineDAO save the serviceLine line items
	}

  /** Saves an existing serviceLine to the database */
  private void update(ServiceLine serviceLine, Connection conn) throws Exception {

	//use a prepared statement to prevent sql injection
	//run the query and set dirty to fase
	//make sure to close the prepared statement

  }

  /** Inserts a new serviceLine into the database */
  private void insert(ServiceLine serviceLine, Connection conn) throws Exception {
    //use a perpared statement
	//set the alreadyInDB variable to true
	//dirty to false
	//close the prepared statement

  }





  /////////////////////////////////////////////////
  ///   DELETE methods

  /** We do not support deleting of business objects in this application */
  public void delete(ServiceLine serviceLine) throws DataException {
    throw new RuntimeException("Nice try. The delete function is not supported in this application.");
  }



  ////////////////////////////////////////////////
  ///   SEARCH methods

  /** Retrieves all dayofweeks from the database */
  public List<ServiceLine> getAll() throws DataException {
    //establish a connection from the pool, use prepared statement, use the search method below to run the statement, catch any errors
	//release the connection

  }

  /** Internal method to search by certain fields */
  private List<ServiceLine> search(PreparedStatement stmt, Connection conn) throws Exception {
    //instantiate a list of serviceLines to add recordsets to. this will make going through the list easier
	//finally, close the statement and return the list

  }



}//class
